﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ActiveStory.Exporter
{
    public class ActiveStoryImageExporter : IActiveStoryExporter
    {

        #region IActiveStoryExporter Members

        public void Export(ActiveStory.DataManager.ActiveStoryProject project, string path, int startupPage, params string[] parameters)
        {
            if (parameters.Length == 2)        //Single page
            {
                string tempFilePath = Path.GetTempFileName();
                File.Copy(ActiveStory.IO.FileManager.GetAbsolutePath(parameters[0]), tempFilePath, true);
                System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage(new Uri(tempFilePath));
                FileStream fs = new FileStream(path, FileMode.Create);
                switch (parameters[1])
                {
                    case "PNG":
                        System.Windows.Media.Imaging.PngBitmapEncoder pbe = new System.Windows.Media.Imaging.PngBitmapEncoder();
                        pbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                        pbe.Save(fs);
                        break;
                    case "GIF":
                        System.Windows.Media.Imaging.GifBitmapEncoder gbe = new System.Windows.Media.Imaging.GifBitmapEncoder();
                        gbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                        gbe.Save(fs);
                        break;
                    case "JPG":
                        System.Windows.Media.Imaging.JpegBitmapEncoder jbe = new System.Windows.Media.Imaging.JpegBitmapEncoder();
                        jbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                        jbe.Save(fs);
                        break;
                    case "BMP":
                        System.Windows.Media.Imaging.BmpBitmapEncoder bbe = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                        bbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                        bbe.Save(fs);
                        break;
                    case "TIFF":
                        System.Windows.Media.Imaging.TiffBitmapEncoder tbe = new System.Windows.Media.Imaging.TiffBitmapEncoder();
                        tbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                        tbe.Save(fs);
                        break;
                    default:
                        break;
                }
                fs.Flush();
                fs.Close();
            }
            else if (parameters.Length == 1)        //All Images
            {
                foreach (ActiveStory.DataManager.ActiveStoryPageMetadata pageItem in project.PageLookupTable.Values)
                {
                    System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage(new Uri(pageItem.TempThumbnailPath));
                    FileStream fs = new FileStream(Path.Combine(path, project.ProjectName+"-"+pageItem.PageNumber+"."+parameters[0].ToLower()), FileMode.Create);
                    switch (parameters[0])
                    {
                        case "PNG":
                            System.Windows.Media.Imaging.PngBitmapEncoder pbe = new System.Windows.Media.Imaging.PngBitmapEncoder();
                            pbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                            pbe.Save(fs);
                            break;
                        case "GIF":
                            System.Windows.Media.Imaging.GifBitmapEncoder gbe = new System.Windows.Media.Imaging.GifBitmapEncoder();
                            gbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                            gbe.Save(fs);
                            break;
                        case "JPG":
                            System.Windows.Media.Imaging.JpegBitmapEncoder jbe = new System.Windows.Media.Imaging.JpegBitmapEncoder();
                            jbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                            jbe.Save(fs);
                            break;
                        case "BMP":
                            System.Windows.Media.Imaging.BmpBitmapEncoder bbe = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                            bbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                            bbe.Save(fs);
                            break;
                        case "TIFF":
                            System.Windows.Media.Imaging.TiffBitmapEncoder tbe = new System.Windows.Media.Imaging.TiffBitmapEncoder();
                            tbe.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bi));
                            tbe.Save(fs);
                            break;
                        default:
                            break;
                    }
                    fs.Flush();
                    fs.Close();
                }
            }
        }

        #endregion
    }
}
